<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/23
 * Time: 14:48
 */
class Model_Index extends PhalApi_Model_NotORM{
    protected function getTableName($id){
        return 't_user';
    }
    /*
     * 发送用户信息  手机号码入库
     */
    public function getByPhone($obj){
        $time=time();
        //$_SESSION['phone'] = $obj->phone;   //把手机号码存入session
        $data=array(
            'landtime' => $time,
            'phone'  => $obj->phone,
        );
        $this->getORM()->insert($data);
    }
    //查询手机号当天 是否已经插入
    public function isPhone($obj){
        $time = date('Ymd',time());
        $phone=$obj->phone;
        return $this->getORM()->select('*')->where("phone=? and FROM_UNIXTIME(landtime,'%Y%m%d')=$time",$phone)->fetchAll();
    }

    /**
     * 用户当天抽奖    判断用户当天是否中过奖
     */
    public function getGift($obj){
        $phone = $obj->phone;
        $time = date('Ymd',time());
        return $this->getORM()->select('*')->where("phone=? and prizetime=$time and FROM_UNIXTIME(landtime,'%Y%m%d')=$time",$phone)->fetchAll();
    }
    /*
     * 用户中奖把中奖时间当天入库
     */
    public function writePrize($obj){
        $phone = $obj->phone;
        $time = date('Ymd',time());
        $this->getORM()->where("phone=? and FROM_UNIXTIME(landtime,'%Y%m%d')=$time",$phone)
            ->update(array('prizetime'=>$time));
    }
    /*
     * 判断用户当天是否有中奖机会
     */
    public function isChance($obj){
        $phone = $obj->phone;
        $time = date('Ymd',time());
       return $this->getORM()->select('lottery')->where("phone=? and FROM_UNIXTIME(landtime,'%Y%m%d')=$time",$phone)
              ->fetchAll();
    }
    /*
     * 用户中奖，机会减 1
     */
    public function decLottery($obj){
        $phone = $obj->phone;
        $time = date('Ymd',time());
        $this->getORM()->where("phone=? and FROM_UNIXTIME(landtime,'%Y%m%d')=$time and prizetime=$time",$phone)
             ->update(array('lottery'=>new NotORM_Literal("lottery - 1")));
    }
}